namespace cnVery.Cms
{
    using cnVery.Data;
    using cnVery.Utils;
    using System;
    using System.Data;
    using System.Text;

    public class news
    {
        public static void Add(newsitem model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into news(");
            builder.Append("[categoryid],[groups],[power],[money],[experience],[title],[keywords],[body],[username],[issuedate],[expiredate],[addeddate],[clickcount],[vouch],[approved],[deleted],[picture]");
            builder.Append(")");
            builder.Append(" values (");
            builder.Append("" + model.categoryid + ",");
            builder.Append("'" + model.groups + "',");
            builder.Append("" + model.power + ",");
            builder.Append("" + model.money + ",");
            builder.Append("" + model.experience + ",");
            builder.Append("'" + model.title + "',");
            builder.Append("'" + model.keywords + "',");
            builder.Append("'" + model.body + "',");
            builder.Append("'" + model.username + "',");
            builder.Append("'" + model.issuedate + "',");
            builder.Append("'" + model.expiredate + "',");
            builder.Append("'" + model.addeddate + "',");
            builder.Append("" + model.clickcount + ",");
            builder.Append("" + (model.vouch ? 1 : 0) + ",");
            builder.Append("" + (model.approved ? 1 : 0) + ",");
            builder.Append("" + (model.deleted ? 1 : 0) + ",");
            builder.Append("'" + model.picture + "'");
            builder.Append(")");
            Connection connection = new Connection();
            connection.Execute(builder.ToString());
            connection.Close();
        }

        public static void Delete(newsitem model)
        {
            Tools.deletefiles(model.body);
            Tools.deletefile(model.picture);
        }

        public static void Delete(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete news ");
            builder.Append(" where id=" + id);
            Connection connection = new Connection();
            connection.Execute(builder.ToString());
            connection.Close();
        }

        public static bool Exists(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from news where id=" + id + "");
            Connection connection = new Connection();
            object scalar = connection.GetScalar(builder.ToString());
            connection.Close();
            if (object.Equals(scalar, null) || object.Equals(scalar, DBNull.Value))
            {
                return false;
            }
            int num = (int) scalar;
            return (num > 0);
        }

        public static int GetCount(string filter)
        {
            if (filter == "")
            {
                filter = "0=0";
            }
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from news where " + filter);
            Connection connection = new Connection();
            object scalar = connection.GetScalar(builder.ToString());
            connection.Close();
            if (object.Equals(scalar, null) || object.Equals(scalar, DBNull.Value))
            {
                return 0;
            }
            return (int) scalar;
        }

        public static DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from news ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by id desc");
            Connection connection = new Connection();
            DataTable dataTable = connection.GetDataTable(builder.ToString());
            connection.Close();
            return dataTable;
        }

        public static DataTable GetList(int pageindex, int pagesize, string filter)
        {
            if (pageindex <= 0)
            {
                return null;
            }
            if (filter == "")
            {
                filter = "0=0";
            }
            string sql = string.Concat(new object[] { "select top ", pagesize, " * from news where ", filter });
            if (pageindex > 1)
            {
                object obj2 = sql;
                sql = string.Concat(new object[] { obj2, " and id < (select min(id) from (select top ", pagesize * (pageindex - 1), " id from news where ", filter, " order by id desc) as tb)" });
            }
            sql = sql + " order by id desc ";
            Connection connection = new Connection();
            DataTable dataTable = connection.GetDataTable(sql);
            connection.Close();
            return dataTable;
        }

        public static newsitem GetModel(int id)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from news ");
            builder.Append(" where id=" + id);
            newsitem newsitem = new newsitem();
            Connection connection = new Connection();
            DataTable dataTable = connection.GetDataTable(builder.ToString());
            connection.Close();
            newsitem.id = id;
            if (dataTable.Rows.Count <= 0)
            {
                return null;
            }
            if (dataTable.Rows[0]["categoryid"].ToString() != "")
            {
                newsitem.categoryid = int.Parse(dataTable.Rows[0]["categoryid"].ToString());
            }
            newsitem.groups = dataTable.Rows[0]["groups"].ToString();
            if (dataTable.Rows[0]["power"].ToString() != "")
            {
                newsitem.power = int.Parse(dataTable.Rows[0]["power"].ToString());
            }
            if (dataTable.Rows[0]["money"].ToString() != "")
            {
                newsitem.money = int.Parse(dataTable.Rows[0]["money"].ToString());
            }
            if (dataTable.Rows[0]["experience"].ToString() != "")
            {
                newsitem.experience = int.Parse(dataTable.Rows[0]["experience"].ToString());
            }
            newsitem.title = dataTable.Rows[0]["title"].ToString();
            newsitem.keywords = dataTable.Rows[0]["keywords"].ToString();
            newsitem.body = dataTable.Rows[0]["body"].ToString();
            newsitem.username = dataTable.Rows[0]["username"].ToString();
            if (dataTable.Rows[0]["issuedate"].ToString() != "")
            {
                newsitem.issuedate = DateTime.Parse(dataTable.Rows[0]["issuedate"].ToString());
            }
            if (dataTable.Rows[0]["expiredate"].ToString() != "")
            {
                newsitem.expiredate = DateTime.Parse(dataTable.Rows[0]["expiredate"].ToString());
            }
            if (dataTable.Rows[0]["addeddate"].ToString() != "")
            {
                newsitem.addeddate = DateTime.Parse(dataTable.Rows[0]["addeddate"].ToString());
            }
            if (dataTable.Rows[0]["clickcount"].ToString() != "")
            {
                newsitem.clickcount = int.Parse(dataTable.Rows[0]["clickcount"].ToString());
            }
            if (dataTable.Rows[0]["vouch"].ToString() != "")
            {
                if ((dataTable.Rows[0]["vouch"].ToString() == "1") || (dataTable.Rows[0]["vouch"].ToString().ToLower() == "true"))
                {
                    newsitem.vouch = true;
                }
                else
                {
                    newsitem.vouch = false;
                }
            }
            if (dataTable.Rows[0]["approved"].ToString() != "")
            {
                if ((dataTable.Rows[0]["approved"].ToString() == "1") || (dataTable.Rows[0]["approved"].ToString().ToLower() == "true"))
                {
                    newsitem.approved = true;
                }
                else
                {
                    newsitem.approved = false;
                }
            }
            if (dataTable.Rows[0]["deleted"].ToString() != "")
            {
                if ((dataTable.Rows[0]["deleted"].ToString() == "1") || (dataTable.Rows[0]["deleted"].ToString().ToLower() == "true"))
                {
                    newsitem.deleted = true;
                }
                else
                {
                    newsitem.deleted = false;
                }
            }
            newsitem.picture = dataTable.Rows[0]["picture"].ToString();
            return newsitem;
        }

        public static void Update(newsitem model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update news set ");
            builder.Append("[categoryid]=" + model.categoryid + ",");
            builder.Append("[groups]='" + model.groups + "',");
            builder.Append("[power]=" + model.power + ",");
            builder.Append("[money]=" + model.money + ",");
            builder.Append("[experience]=" + model.experience + ",");
            builder.Append("[title]='" + model.title + "',");
            builder.Append("[keywords]='" + model.keywords + "',");
            builder.Append("[body]='" + model.body + "',");
            builder.Append("[username]='" + model.username + "',");
            builder.Append("[issuedate]='" + model.issuedate + "',");
            builder.Append("[expiredate]='" + model.expiredate + "',");
            builder.Append("[addeddate]='" + model.addeddate + "',");
            builder.Append("[clickcount]=" + model.clickcount + ",");
            builder.Append("[vouch]=" + (model.vouch ? 1 : 0) + ",");
            builder.Append("[approved]=" + (model.approved ? 1 : 0) + ",");
            builder.Append("[deleted]=" + (model.deleted ? 1 : 0) + ",");
            builder.Append("[picture]='" + model.picture + "'");
            builder.Append(" where id=" + model.id + "");
            Connection connection = new Connection();
            connection.Execute(builder.ToString());
            connection.Close();
        }

        public static void Update(string sql, string ids)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update [news] set " + sql + " where id in (" + ids + ")");
            Connection connection = new Connection();
            connection.Execute(builder.ToString());
            connection.Close();
        }
    }
}

